package com.assignwork.modules.inportship;

import com.assignwork.dto.ShipForecastSearchDTO;
import com.assignwork.dto.VoyageInfoSubmitDTO;
import com.assignwork.entity.VoyageInfo;
import com.assignwork.service.IVoyageInfoService;
import com.assignwork.service.transport.IShipForecastService;
import com.assignwork.vo.ShipVoyagePageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

/**
 * @ClassName VoyageInfoController
 * @Description 船舶信息总页控制器
 * @Author dujianyu
 * @Date 2023/8/15 9:37
 */
@RestController
@AllArgsConstructor
@RequestMapping("voyageInfo")
public class VoyageInfoController extends BladeController {

    private IVoyageInfoService service;


    @GetMapping("/shipVoyagePage")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页", notes = "传入dto")
    public R<IPage<ShipVoyagePageVO>> shipVoyagePage(ShipForecastSearchDTO dto, Query query) {
        return R.data(service.selectVoyagePage(dto, Condition.getPage(query)));
    }

    @PostMapping("submit")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "添加新的船航次信息", notes = "传入dto")
    public R<String> submitVoyageInfo(@RequestBody VoyageInfoSubmitDTO dto) {
        return R.data(service.submitVoyageInfo(dto));
    }

    @PostMapping("simpleSubmit")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "添加新的船航次信息", notes = "传入dto")
    public R<Boolean> simpleSubmit(@RequestBody VoyageInfoSubmitDTO dto) {
        VoyageInfo data = service.getById(dto.getVoyageId());
        data.setVisaTon(dto.getVisaTon());
        data.setIsBudget(dto.getIsBudget());
        data.setIsFinish(dto.getIsFinish());
        return R.status(service.saveOrUpdate(data));
    }

    @PostMapping("preFinishTonSubmit")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "预做吨结吨更新", notes = "传入dto")
    public R<Boolean> preFinishTonSubmit(@RequestBody VoyageInfoSubmitDTO dto) {
        VoyageInfo data = service.getById(dto.getVoyageId());
        if (Func.isNull(data) || Func.isNull(data.getId())) {
            throw new RuntimeException("没有voyageId,请在船舶在港情况点击此船明细，刷新后再录入吨数");
        }
        data.setPreTon(dto.getShipPreTon());
        data.setFinishTon(dto.getShipFinishTon());
        return R.status(service.saveOrUpdate(data));
    }

    @PostMapping("submitShow")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "更新是否显示", notes = "传入dto")
    public R<Boolean> submitShow(@RequestBody VoyageInfoSubmitDTO dto) {
        return R.status(service.submitShow(dto));
    }
}
